package com.example.downloaddemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.os.Handler;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

	private Button go;
	private EditText edit;
	private ListView listview;
	private ProgressBar progress;
	private String url = "http://gdown.baidu.com/data/wisegame/4f9b25fb0e093ac6/QQ_220.apk";
	private String path;
	private HashMap<String, DownLoader> loaders;
	private LoadInfo info;
	private List<String> urllist;
	private ProgressAdapter adapter;
	private HashMap<String, View> views;
	private HashMap<String, ProgressBar> bars;
	//private HashMap<String, LoadInfo> infos;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case -1://发生网络错误
					Toast.makeText(MainActivity.this, "网络解析错误", Toast.LENGTH_SHORT).show();
					break;
				case 0://解析网址完毕
					adapter.notifyDataSetChanged();
					break;
				case 1://更新progressbar
					//progress = (ProgressBar) views.get(msg.obj).findViewById(R.id.progress);
					progress=bars.get(msg.obj);
				//	Log.i("msg","aa:"+progress.getProgress());
					progress.incrementProgressBy(msg.arg1);
					listview.invalidate();
				//	Log.i("msg","len:"+msg.arg1);
					if (progress.getProgress() >= progress.getMax()) {
						loaders.get(msg.obj).delete();
						loaders.remove(msg.obj);
						views.remove(msg.obj);
						bars.remove(msg.obj);
						Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
					}


					break;
				case 2://初始化progressbar
					info = (LoadInfo) msg.obj;
					//progress = (ProgressBar) views.get(info.getUrlstr()).findViewById(R.id.progress);
					progress=bars.get(info.getUrlstr());
					//Log.i("msg","--"+progress+"----"+info);
					progress.setMax(info.getFilesize());
					progress.setProgress(info.getComplete());
					break;

			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		adapter = new ProgressAdapter(urllist, handler, loaders, views,bars);
		listview.setAdapter(adapter);


		go.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				urllist.clear();
				adapter.notifyDataSetChanged();
				url = edit.getText().toString();
				new ListThread(url, urllist, handler).start();//解析网址
			}
		});


	}

	private void initView() {
		go = (Button) findViewById(R.id.but_go);
		edit = (EditText) findViewById(R.id.edit);
		listview = (ListView) findViewById(R.id.listview);
		urllist = new ArrayList<String>();
		loaders = new HashMap<String, DownLoader>();
		views = new HashMap<String, View>();
		//infos = new HashMap<String, LoadInfo>();
		bars=new HashMap<String, ProgressBar>();
	}


}
